package com.friend.base.controller.base;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.friend.base.service.base.CommentService;
import com.friend.base.service.base.SpaceService;
import com.friend.common.model.BaseController;
import com.friend.common.model.Result;
import com.friend.entity.entity.base.Comment;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author friend
 * @since 2019-10-17
 */
@Api(tags = {"评价前台控制器"})
@RestController
@RequestMapping(value = "/comment", produces = {MediaType.APPLICATION_JSON_UTF8_VALUE})
public class CommentController extends BaseController {

    @Autowired
    private SpaceService spaceService;
    @Autowired
    private CommentService commentService;

    @GetMapping("/getSpaceComment")
    @ApiOperation(value = "获取场子评论", notes = "根据场子主键获取场子评论", httpMethod = "GET")
    public Result getSpaceComment(@RequestParam("spaceId") String spaceId){
        try {
            List<Comment> comments = commentService.list(new QueryWrapper<Comment>().eq("spaceId", spaceId).orderByDesc("create_time"));
            comments.forEach(comment -> comment.setSpaceTitle(spaceService.getById(comment.getSpace()).getTitle()));
            return createSuccessResult(comments);
        }catch (Exception e){
            e.printStackTrace();
            return this.createFailedResult("评论列表查询失败");
        }
    }
}

